package org.example.forum.services.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.dao.BoardMapper;
import org.example.forum.exception.ApplicationException;
import org.example.forum.model.Board;
import org.example.forum.services.IBoardService;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //参数校验
        if(num < 0){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO查询数据库中的信息
        List<Board> boardList = boardMapper.selectByNum(num);
        //返回结果
        return boardList;
    }


    @Override
    public Board selectById(Long id) {
        //对板块进行非空校验
        if(id == null || id <=0){
            //打印日志
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
        
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //对板块进行非空校验
        if(id == null || id <=0){
            //打印日志 
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //查询板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString()+"board id"+id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));

        }
        //帖子数加一 需创建一个新的board 避免更新所有值
        Board updateBoard = new Board();
        //设置要更新的属性
        updateBoard.setId(board.getId());
        //帖子数加一
        updateBoard.setArticleCount(board.getArticleCount()+1);
        //调用DAO 动态更新
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //校验一些变更的数量是否为1 为1正常不为1 错误
        if(row != 1 ){
            log.warn(ResultCode.FAILED+"受影响的行数不为1"+" ,id: "+id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        // 非空校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查询版块详情
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ", board id = " + id);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        // 构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        // 判断减1之后是否小于0
        if (updateBoard.getArticleCount() < 0) {
            // 如果小于0那么设置为0
            updateBoard.setArticleCount(0);
        }

        // 调用DAO
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
